import React from 'react';
import Grid from '../../components/table/grid';
import tranStartType from '../../utils/tranStartType';
import RecentForm from '../order/recentForm';
import {sessionManager} from '../../utils/sessionStorage';
import moment from 'moment';
import OrderDetail from './orderDetail';
import SassNotification from '../../components/notification/notification'
import { injectIntl } from 'react-intl';

class recentCharge extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      params:{
        searchTimeStart:moment().subtract('days',1).format('YYYY.MM.DD HH:mm:ss'),
        searchTimeEnd:moment().format('YYYY.MM.DD HH:mm:ss'),
      },
      refresh:0,
      gridStyle:{
        scroll:true
      }
    };
    const common = this.props.intl.messages.common;
    this.columns = [
      {
        title: common.orderNO,
        dataIndex: 'id',
        key: 'id',
        width: 320,
        fixed: 'left'
      },
      {
        title: common.chargeMethod,
        dataIndex: 'appCompanyName',
        key: 'appCompanyName',
        width: 180,
      },
      {
        title: common.stubGroupName,
        dataIndex: 'stubGroupName',
        key: 'stubGroupName',
        width: 180,
      },
      {
        title: common.stubNo,
        dataIndex: 'stubId',
        key: 'stubId',
        width: 180,
      },
      {
        title: common.chargingStartTime,
        dataIndex: 'timeStart',
        key: 'timeStart',
        width: 150,
      },
      {
        title: common.chargingEndTime,
        dataIndex: 'timeEnd',
        key: 'timeEnd',
        width: 150,
      },
      {
        title: common.chargingTimeSecond,
        dataIndex: 'timeDiff',
        key: 'timeDiff',
        width: 150,
      },
      {
        title: common.energy,
        dataIndex: 'power',
        key: 'power',
        width: 150,
      },
      {
        title: common.energyChargeUnit,
        dataIndex: 'feeElectric',
        key: 'feeElectric',
        width: 180,
      },
      {
        title: common.serviceChargeUnit,
        dataIndex: 'feeService',
        key: 'feeService',
        width: 150,
      },
      {
        title: common.totalChargeCost,
        dataIndex: 'feeTotal',
        key: 'costfeeTotal',
        width: 150,
        className:'SC-col-dots'
      },
      {
        title: common.orderState,
        dataIndex: 'status',
        key: 'status',
        width: 150,
      },
      {
        title: common.endReason,
        dataIndex: 'endInfo',
        key: 'endInfo',
        width: 200,
        className:'SC-col-dots'
      },
      {
        title: common.chargeType,
        dataIndex: 'chargeType',
        key: 'chargeType',
        width: 200,
        className:'SC-col-dots',
        	render:(text,record)=>{
                    switch (record.chargeType) {
                    						case 0:
                    							return '交流';
                    						case 1:
                    							return '直流';
                    						default :
                    							break
                    					}
                    				}
      },
      {
        title: common.operation,
        key: 'operation',
        fixed: 'right',
        width: 100,
        render: (text, record) => (
          <span>
                        <a  onClick={()=>this.showModalX('orderDetail', true, record.id)}>{common.prevewDetail}</a>
                    </span>
        ),
      }];
  }
  formSubmit=(data)=>{
    this.setState({
      params: {...data},
      refresh: this.state.refresh + 1
    });
  }
  getTableData=(data)=>{
    this.setState({
      TableData:data
    })
  }
  transformData=(data)=>{
    const common = this.props.intl.messages.common;
    data.forEach((item,index)=>{
      if(item.status===1){
        item.status=common.charging;
      }else{
        item.status=common.chargingCompleted;
      }
      item.startType=tranStartType(item.startType)
      return item
    });
    return data;
  }

  showModalX(key, value, orderId, record) {
    this.setState({
      [key]: value,
      orderId: orderId,
      selectInfo: record
    })
  }

  showModal(key, value, isRefresh) {
    this.setState({
      [key]: value,
      refresh: this.state.refresh + 1
    })
  }

  render(){
    return(
      <div>
        <SassNotification></SassNotification>
        <RecentForm  params={this.state.params} formSubmit={this.formSubmit}></RecentForm>
        <Grid gridStyle={this.state.gridStyle} transformData={this.transformData} refresh={this.state.refresh} params={this.state.params} columns={this.columns} url="api/order/latelyOrderList" />
        <OrderDetail visible={this.state.orderDetail} orderId={this.state.orderId}
                     close={() => this.showModal('orderDetail', false, true)}/>
      </div>
    )
  }
}

export default injectIntl(recentCharge)

